using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class MailTemplate:Base
    {
       
        private DAL.MailTemplate dal = null;

        public MailTemplate()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal=new DAL.MailTemplate(isession);
        }

        public Common.Result Add(Model.MailTemplate value)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");
            if (dal.GetModel(value.Code) != null)
            {
                ret.message = "[编号]已经存在";
                return ret;
            }
            ret = dal.Add(value);
            return ret;
        }

        public Common.Result Update(Model.MailTemplate value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.Code);
            if (dalvalue == null)
            {
                ret.message = "数据不存在";
                return ret;
            }
            dalvalue.Subject = value.Subject;
            dalvalue.Name = value.Name;
            dalvalue.Body = value.Body;
            dalvalue.Notes = value.Notes;
            ret=dal.Update(dalvalue);
            if (ret.success) //删除解析后的模板缓存
            {
                string subjectkey = Common.Keys.MailTemplateSubject + value.Code;
                string bodykey = Common.Keys.MailTemplateBody + value.Code;
                Common.CacheHelper.Remove(subjectkey);
                Common.CacheHelper.Remove(bodykey);
            }
            return ret;
        }

        public Common.Result Delete(string ids)
        {

            var ret=dal.Delete(ids);
            return ret;
        }

        public Model.MailTemplate Get(string code)
        {
            return dal.GetModel(code);
        }

        public IList<Model.MailTemplate> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length, ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }
    }
}
